package com.xzqwjw.taskmanager.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.leaderinfo.model.InfoFamily;

public class FamilyDao implements com.leaderinfo.service.OperateDatabaseService {

	private SQLiteDatabase objDB;
	private String strDBFile = "";

	public FamilyDao(String strDBFile) {
		this.strDBFile = strDBFile;
	}

	@Override
	public Map<String, Object> getRecordByAID(int intAID) {
		InfoFamily objFamilyInfo = null;
		Map<String, Object> mapInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_family WHERE aid=?";
		String[] aryArgs = { String.valueOf(intAID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		while (objCursor.moveToNext()) {
			objFamilyInfo = new InfoFamily();
			getInfo(objCursor, objFamilyInfo);
			mapInfo = new HashMap<String, Object>();
			mapInfo.put("FamilyInfo", objFamilyInfo);
		}
		objCursor.close();
		objDB.close();
		return mapInfo;
	}

	@Override
	public Map<String, Object> getRecordByBasicID(int intBasicID) {
		return null;
	}

	@Override
	public List<Map<String, Object>> getRecordsByBasicID(int intBasicID) {
		List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapInfo = null;
		InfoFamily objFamilyInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_family WHERE id_basic=?";
		String[] aryArgs = { String.valueOf(intBasicID) };
		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapInfo = new HashMap<String, Object>();
				objFamilyInfo = new InfoFamily();
				getInfo(objCursor, objFamilyInfo);
				mapInfo.put("FamilyInfo", objFamilyInfo);
				listInfo.add(mapInfo);
			}
		}
		objCursor.close();
		objDB.close();
		return listInfo;
	}

	@Override
	public List<Map<String, Object>> getAllRecords() {
		List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapInfo = null;
		InfoFamily objFamilyInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_family";
		Cursor objCursor = objDB.rawQuery(strSQL, null);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapInfo = new HashMap<String, Object>();
				objFamilyInfo = new InfoFamily();
				getInfo(objCursor, objFamilyInfo);
				mapInfo.put("FamilyInfo", objFamilyInfo);

				listInfo.add(mapInfo);
			}
		}
		objCursor.close();
		objDB.close();
		return listInfo;
	}

	public List<Map<String, Object>> getPageRecords(int intCurrentPage,
			int intPageSize) {
		List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapInfo = null;
		InfoFamily objFamilyInfo = null;

		int intFirstResult = (intCurrentPage - 1) * intPageSize;
		int intMaxResult = intCurrentPage * intPageSize;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_family LIMIT ?,?";
		String[] aryArgs = { String.valueOf(intFirstResult),
				String.valueOf(intMaxResult) };
		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.getCount() > 0) {
			while (objCursor.moveToNext()) {
				mapInfo = new HashMap<String, Object>();
				objFamilyInfo = new InfoFamily();
				getInfo(objCursor, objFamilyInfo);
				mapInfo.put("FamilyInfo", objFamilyInfo);

				listInfo.add(mapInfo);
			}
		}
		objCursor.close();
		objDB.close();
		return listInfo;
	}

	private void getInfo(Cursor objCursor, InfoFamily objFamilyInfo) {
		objFamilyInfo.AID = objCursor.getInt(objCursor.getColumnIndex("aid"));
		objFamilyInfo.BasicID = objCursor.getInt(objCursor
				.getColumnIndex("id_basic"));
		objFamilyInfo.Appellation = objCursor.getString(objCursor
				.getColumnIndex("appellation"));
		objFamilyInfo.RealName = objCursor.getString(objCursor
				.getColumnIndex("realname"));
		objFamilyInfo.Birthday = objCursor.getString(objCursor
				.getColumnIndex("birthday"));
		objFamilyInfo.PoliticalAffiliation = objCursor.getString(objCursor
				.getColumnIndex("political_affiliation"));
		objFamilyInfo.WorkUnit = objCursor.getString(objCursor
				.getColumnIndex("work_unit"));
	}

	@Override
	public List<Map<String, Object>> getRecordsByWhere(String strWhere) {
		return null;
	}

}
